<?php
/**
 * Copyright (C) 2010-2013 Oscar Valenzuela B. (ovalenzuela@xpertians.com)
 * 
 * This file is part of WarpIT.
 * WarpIT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * WarpIT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with WarpIT.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * This class cointains HSS Requests methods for WarpIT
 * @author Oscar Valenzuela <ovalenzuela@xpertians.com>
 * @version 1.0
 * @package WarpIT!
 * @copyright Copyright (C) 2010-2013 Xpertians SpA
 * @license GNU GPLv3 or later
 * @link http://www.xpertians.com
 */
 
Class Request_HLR{

	/**
	 * Config information
	 *
	 * @var mixed
	 */
	private $config;
	
	/**
	 * Script response
	 *
	 * @var mixed
	 */
	private $response;

	/**
	 * Request Get info from HSS and HLR provisioning
	 *
	 */
	function Request_HLR($config){
		$this->config	= $config;
	}
	
	/**
	 * Request_FNR is the constructor of this class
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $MSISDN Phone number
	 * @return array
	 */
	function Request_FNR($MSISDN){
		foreach($this->config['fnr'] AS $fnr){
			$cmdExec			= $this->config['basepath']."Vendors/HLR_Online/fnr.sh '".$fnr."' ".$MSISDN." ".$this->config['user']." ".$this->config['pass'];
			shell_exec("/bin/bash ".$cmdExec);
			$this->response['rsp']	= trim(shell_exec("cat ".$this->config['basepath']."Vendors/HLR_Online/Cache/fnr_".$MSISDN.".log | grep '730'"));
			shell_exec("rm -rf ".$this->config['basepath']."Vendors/HLR_Online/Cache/fnr_".$MSISDN.".log");
			if($this->response['rsp']!=""){
				break;
			}
		}
		return $this->response;
		
	}
	
	/**
	 * Request_HLRQRY is the constructor of this class
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $MSISDN Phone number
	 * @param string $IMSI IMSI number
	 * @return array
	 */
	function Request_HLRQRY($MSISDN,$IMSI){
		include_once("Vendors/HLR_Online/Classes/Request_HEL.php");
		$HLRid		= getImsiRange($IMSI['rsp']);
		$HLRinfo	= getHlrFromID($HLRid);
		$cmdExec	= $this->config['basepath']."Vendors/HLR_Online/hlr.sh '".$HLRinfo['ip']."' ".$MSISDN." ".$this->config['user']." ".$this->config['pass'];
		shell_exec("/bin/bash ".$cmdExec);
		$logFile	= file_get_contents($this->config['basepath']."Vendors/HLR_Online/Cache/hlr_".$MSISDN.".log");
		shell_exec("rm -rf ".$this->config['basepath']."Vendors/HLR_Online/Cache/hlr_".$MSISDN.".log");
		return $logFile;
	}

	/**
	 * Request_HLRINFO is the constructor of this class
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $MSISDN Phone number
	 * @param string $IMSI IMSI number
	 * @return array
	 */
	function Request_HLRINFO($MSISDN,$IMSI){
		include_once("Vendors/HLR_Online/Classes/Request_HEL.php");
		$HLRid			= getImsiRange($IMSI);
		$HLRinfo		= getHlrFromID($HLRid);
		$arrRst			= array();
		$arrRst['IMSI']		= $IMSI;
		$arrRst['HLRID']	= $HLRid;
		$arrRst['HLRINFO']	= $HLRinfo;
		return $arrRst;
	}

	/**
	 * Parse_HLRQRY is the constructor of this class
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $content Log content
	 * @return array
	 */
	function Parse_HLRQRY($content){
		$contentArr	= split("\n", $content);
		$finalContent	= array();
		$start		= 0;
		$nextStep	= "none";
		foreach($contentArr AS $line){
			if(strstr($line,"END")){
				break;
			}
			if($start){
				if (preg_match("/NOT CONNECTED/",trim($line))){
					$finalContent['STATUS']	= "IMSI_ERROR";
					break;
				}elseif ($nextStep=="SUSCRIBER_IDENTITY"){
					$arrTmp		= explode(" ",trim($line));
					$position	= 0;
					foreach($arrTmp AS $tmp){
						if(trim($tmp)!=""){
							switch($position){
								case 0:
									$finalContent['SUSCRIBER_IDENTITY']['MSISDN']	= trim($tmp);
								break;
								case 1:
									$finalContent['SUSCRIBER_IDENTITY']['IMSI']	= trim($tmp);
								break;
								case 2:
									$finalContent['SUSCRIBER_IDENTITY']['STATE']	= trim($tmp);
								break;
								case 3:
									$finalContent['SUSCRIBER_IDENTITY']['AUTHD']	= trim($tmp);
									$nextStep					= "NAM";
								break;
							}
							$position++;
						}
					}
				}elseif ($nextStep=="NAM"){
					if (!preg_match("/NAM/",trim($line))){
						switch(intval(trim($tmp))){
							case 0:
								$finalContent['NAM']	= "VOICE - DATA";
							break;
							case 1:
								$finalContent['NAM']	= "VOICE";
							break;
							case 2:
								$finalContent['NAM']	= "DATA";
							break;
						}
						$finalContent['OBO2']		= 0;
						$finalContent['OBR1']		= 0;
						$finalContent['TS211']		= 0;
						$finalContent['TS221']		= 0;
						$finalContent['OCSI1']		= 0;
						$finalContent['OFA1']		= 0;
						$finalContent['OFA2']		= 0;
						$finalContent['OFA4']		= 0;
						$finalContent['OICK100']	= 0;
						$finalContent['OICK103']	= 0;
						$finalContent['CFNRY1']		= 0;
						$finalContent['CAW1']		= 0;
						$finalContent['CLIP1']		= 0;
						$finalContent['CLIR1']		= 0;
						$finalContent['OSB31']		= 0;
						$finalContent['OBI1']		= 0;
						$finalContent['OBO1']		= 0;
						$finalContent['TICK110']	= 0;
						$finalContent['HOLD1']		= 0;
						$finalContent['PRBT1']		= 0;
						$finalContent['STYPE1']		= 0;
						$finalContent['STYPE14']	= 0;
						$nextStep			= "PERMANENT";
					}
				}elseif ($nextStep=="PERMANENT"){
					if (strstr(trim($line),"AMSISDN")){
						$nextStep			= "SUPPLEMENTARY";
						$finalContent['CFU']		= 0;
						$finalContent['CFB']		= 0;
						$finalContent['CFNRY']		= 0;
						$finalContent['CFNRC']		= 0;
					}else{
						if(preg_match("/(OBO-2)/", trim($line))){ $finalContent['OBO2']		= 1; }
						if(preg_match("/(OBR-1)/", trim($line))){ $finalContent['OBR1']		= 1; }
						if(preg_match("/(TS21-1)/", trim($line))){ $finalContent['TS211']	= 1; }
						if(preg_match("/(TS22-1)/", trim($line))){ $finalContent['TS221']	= 1; }
						if(preg_match("/(OCSI-1)/", trim($line))){ $finalContent['OCSI1']	= 1; }
						if(preg_match("/(OFA-1)/", trim($line))){ $finalContent['OFA1']		= 1; }
						if(preg_match("/(OFA-2)/", trim($line))){ $finalContent['OFA2']		= 1; }
						if(preg_match("/(OFA-4)/", trim($line))){ $finalContent['OFA4']		= 1; }
						if(preg_match("/(OICK-100)/", trim($line))){ $finalContent['OICK100']	= 1; }
						if(preg_match("/(OICK-103)/", trim($line))){ $finalContent['OICK103']	= 1; }
						if(preg_match("/(CFNRY-1)/", trim($line))){ $finalContent['CFNRY1']	= 1; }
						if(preg_match("/(CAW-1)/", trim($line))){ $finalContent['CAW1']		= 1; }
						if(preg_match("/(CLIP-1)/", trim($line))){ $finalContent['CLIP1']	= 1; }
						if(preg_match("/(CLIR-1)/", trim($line))){ $finalContent['CLIR1']	= 1; }
						if(preg_match("/(OSB3-1)/", trim($line))){ $finalContent['OSB31']	= 1; }
						if(preg_match("/(OBI-1)/", trim($line))){ $finalContent['OBI1']		= 1; }
						if(preg_match("/(OBO-1)/", trim($line))){ $finalContent['OBO1']		= 1; }
						if(preg_match("/(TICK-110)/", trim($line))){ $finalContent['TICK110']	= 1; }
						if(preg_match("/(HOLD-1)/", trim($line))){ $finalContent['HOLD1']	= 1; }
						if(preg_match("/(PRBT-1)/", trim($line))){ $finalContent['PRBT1']	= 1; }
						if(preg_match("/(STYPE-1)/", trim($line))){ $finalContent['STYPE1']	= 1; }
						if(preg_match("/(STYPE-14)/", trim($line))){ $finalContent['STYPE14']	= 1; }
					}
				}elseif ($nextStep=="SUPPLEMENTARY"){
					if (strstr(trim($line),"VLR")){
						$nextStep	= "VLR";
					}else{
						if(preg_match("/(CFU)/", trim($line))){ $finalContent['CFU']		= 1; }
						if(preg_match("/(CFB)/", trim($line))){ $finalContent['CFB']		= 1; }
						if(preg_match("/(CFNRY)/", trim($line))){ $finalContent['CFNRY']	= 1; }
						if(preg_match("/(CFNRC)/", trim($line))){ $finalContent['CFNRC']	= 1; }
					}
				}elseif ($nextStep=="VLR"){
					if (strstr(trim($line),"SGSN")){
						$nextStep	= "SGSN";
					}else{
						if(trim($line)!=""){
							$arrVlr			= explode(" ",trim($line));
							$arrVlrTmp		= explode("-",trim($arrVlr[0]));
							$finalContent['VLR']	= trim($arrVlrTmp[1]);
							$finalContent['MSC']	= trim($arrVlrTmp[1]);
						}
					}
				}elseif ($nextStep=="SGSN"){
					if (strstr(trim($line),"PROTOCOL")){
						$apnId		= 0;
						$nextStep	= "PACKETDATA";
					}else{
						if(trim($line)!=""){
							$arrSgsnTmp		= explode("-",trim($line));
							$finalContent['SGSN']	= trim($arrSgsnTmp[1]);
						}
					}
				}elseif ($nextStep=="PACKETDATA"){
					if (strstr(trim($line),"END")){
						$nextStep	= "END";
						break;
					}else{
						if(trim($line)!=""){
							if (!strstr(trim($line),"APNID")){
								$arrTmp		= explode(" ",trim($line));
								$position	= 0;
								foreach($arrTmp AS $tmp){
									if(trim($tmp)!=""){
										switch($position){
											case 0:
												$finalContent['APNS'][$apnId]['APNID']		= trim($tmp);
												$finalContent['APNS'][$apnId]['APNNAME']		= $this->Translate_APNID(trim($tmp));
											break;
											case 1:
												$finalContent['APNS'][$apnId]['EQOSID']		= $this->Translate_EQOSID(trim($tmp));
											break;
											case 2:
												$finalContent['APNS'][$apnId]['VPAA']		= trim($tmp);
											break;
											case 3:
												$finalContent['APNS'][$apnId]['PDPTY']		= trim($tmp);
											break;
											case 4:
												$finalContent['APNS'][$apnId]['PDPID']		= trim($tmp);
											break;
										}
										$position++;
									}
								}
								$apnId++;
							}
						}
					}
				}elseif (preg_match("/MSISDN/",trim($line))){
					$finalContent['STATUS']	= "OK";
					$nextStep		= "SUSCRIBER_IDENTITY";
				}
			}
			if(strstr($line,"HLR SUBSCRIBER DATA")){
				$start	= true;
			}
		}
		$finalContent['STEP']	= $nextStep;
		$finalContent['HLR']	= $this->Request_HLRINFO($finalContent['SUSCRIBER_IDENTITY']['MSISDN'],$finalContent['SUSCRIBER_IDENTITY']['IMSI']);
		$finalContent['OPER']	= $this->Translate_OPERATOR($finalContent['MSC']);
		return $finalContent;
	}

	/**
	 * Translate_APNID Array for Human reading
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $array Original
	 * @return string
	 */
	function Translate_APNID($APNID){
		include_once("Vendors/HLR_Online/Classes/Request_HEL.php");
		$apnData	= getApnFromID($APNID);
		return $apnData['name'];
	}


	/**
	 * Translate_OPERATOR Array for Human reading
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $array Original
	 * @return string
	 */
	function Translate_OPERATOR($MSC_NUMBER){
		include_once("Vendors/HLR_Online/Classes/Request_HEL.php");
		$operatorData	= getOperatorFromMSC($MSC_NUMBER);
		return $operatorData;
	}

	/**
	 * Translate_QOSTPLID Array for Human reading
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $array Original
	 * @return string
	 */
	function Translate_EQOSID($EQOSID){
		if ($EQOSID == '5') { $veloc="(4352 - 4352)kb"; }
		if ($EQOSID == '6') { $veloc="(128 - 256)kb"; }
		if ($EQOSID == '7') { $veloc="(64 - 64)kb"; }
		if ($EQOSID == '8') { $veloc="(64 - 128)kb"; }
		if ($EQOSID == '9') { $veloc="(64 - 200)kb"; }
		if ($EQOSID == '10'){ $veloc="(64 - 400)kb"; }
		if ($EQOSID == '14'){ $veloc="(8000 - 16000)kb"; }
		if ($EQOSID == '15'){ $veloc="(8000 - 16000)kb"; }
		if ($EQOSID == '16'){ $veloc="(160 - 384)kb"; }
		if ($EQOSID == '17'){ $veloc="(64 - 64)kb"; }
		if ($EQOSID == '18'){ $veloc="(64 - 128)kb"; }
		if ($EQOSID == '19'){ $veloc="(64 - 200)kb"; }
		if ($EQOSID == '20'){ $veloc="(64 - 400)kb"; }
		if ($EQOSID == '25'){ $veloc="(8000 - 42000)kb"; }
		if ($EQOSID == '35'){ $veloc="(8064 - 16000)kb"; }
		if ($EQOSID == '36'){ $veloc="(136 - 256)kb"; }
		if ($EQOSID == '37'){ $veloc="(4096 - 4160)kb"; }
		if ($EQOSID == '40'){ $veloc="(3712 - 3712)kb"; }
		if ($EQOSID == '41'){ $veloc="(5056 - 5056)kb"; }
		if ($EQOSID == '42'){ $veloc="(7168 - 7168)kb"; }
		if ($EQOSID == '50'){ $veloc="(4224 - 4224)kb"; }
		if ($EQOSID == '51'){ $veloc="(4224 - 4224)kb"; }
		if ($EQOSID == '52'){ $veloc="(5504 - 5504)kb"; }
		if ($EQOSID == '53'){ $veloc="(4160 - 4160)kb"; }
		if ($EQOSID == '60'){ $veloc="(5504 - 5504)kb"; }
		if ($EQOSID == '66'){ $veloc="(8000 - 20000)kb"; }
		if ($EQOSID == '67'){ $veloc="(8000 - 22000)kb"; }
		if ($EQOSID == '300'){ $veloc="(3648 - 12000)kb"; }
		if ($EQOSID == '305'){ $veloc="(3584 - 3584)kb"; }
		return $veloc;
	}

}
?>
